<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use DB;
use App\Question;
use App\User;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class QuestionController extends Controller
{
	// 问题详情页面
   public function question($id,$user_id)
   {

   		dd($id);
   		// $question = DB::table('question')->where('id',$id)->first();
   		$question = Question::findOrFail($id);
   		// dd($question);
   		// $users = User::get();
        return view('home.question',['question'=>$question]);

         $user = User::findOrFail($user_id);
         session(['user_id'=>$user_id]);
         //  对关注该问题的时间进行排序
   		$arr = [];
         $i = 0;
   		$question = Question::findOrFail($id);
   		foreach ($question->getFocusByQuestion as $v) {
   			$arr[$i]['nickname'] = $v->detail->nickname;
            $arr[$i]['user_portrait'] = $v->detail->user_portrait;
            $arr[$i]['user_id'] = $v->detail->user_id;
            $arr[$i]['time'] = $v->pivot->create_time;
            $i++;
   		}
   		usort($arr, function($a, $b) {
   			return $b['time'] - $a['time'];
   		});

         // 对问题回答的点赞数进行排序
         $add = [];
         $k = 0;
         $answers = $question->getAnswer->take(20);
         foreach ($answers as $con) {
            $add[$k]['content'] = $con->answer_content;
            $add[$k]['id'] = $con->id;
            $add[$k]['user_id'] = $con->user_id ;
            $add[$k]['num'] = $con->getPraise->where('answer_id', $con->id)->where('apraise_status', 1)->count();
            $add[$k]['praise'] = $con->getPraise->where('answer_id', $con->id)->where('apraise_status', 1)->count();
            $add[$k]['nickname'] = $con->getUserPortrait->nickname;
            $add[$k]['introduction'] = $con->getUserPortrait->introduction;
            $add[$k]['comments'] = $con->getComments->count();
            $add[$k]['length'] = strlen($con->answer_content);
            $add[$k]['portrait'] = $con->getUserPortrait->user_portrait;
            $k++;
         }
         usort($add, function($a, $b) {
            return $b['num'] - $a['num'];
         });

         // dd($add);
         return view('home.question',['question'=>$question, 'user'=>$user, 'question_id'=>$id, 'k'=>$arr, 'an'=>$add]);
   }
}
